/**
 扩展模块
 @author:wcf
 @date:2018.05.26
 @version:1.0
 **/

layui.define(["layer", "jquery"], function (exports) {
    var $ = layui.jquery;
    var shade = 0.5;
    var i_w = 65;
    var obj = {
        form: function (url, w) {//表单编辑
            if (w != undefined) {
                i_w = w;
            }
            if (url.indexOf("?") == -1) {
                url += "?";
            }
            url += "&_t=" + new Date().getTime();
            layer.open({
                type: 2,
                anim: 2,
                title: false,
                shade: shade,
                area: [i_w + '%', '100%'],
                btn: ['确定', '关闭'],
                closeBtn: 0,
                content: url,
                offset: 'r',
                btnAlign: 'c',
                resize: false
                //, skin: 'demo-class'
                ,
                yes: function (index, layero) {
                    var iframeWindow = $(layero).find("iframe")[0].contentWindow;
                    var submitBtn = $(iframeWindow.document.getElementById("submitBtn"));
                    submitBtn.click();
                }
                ,
                end: function (index, layero) {
                    //触发关闭事件后，执行此方法
                }
            });
        }
        , view: function (url, w) {//内容查看
            if (w != undefined) {
                i_w = w;
            }
            if (url.indexOf("?") == -1) {
                url += "?";
            }
            layer.open({
                type: 2,
                anim: 2,
                title: false,
                shade: shade,
                area: [i_w + '%', '100%'],
                btn: ['<i class="layui-icon">&#x1006;</i>&nbsp;关闭'],
                closeBtn: 0,
                resize: false,
                offset: 'r',
                btnAlign: 'c',
                content: url
            });
        } ,
        viewUpdate: function (url, w) {//内容查看
            if (w != undefined) {
                i_w = w;
            }
            if (url.indexOf("?") == -1) {
                url += "?";
            }
            layer.open({
                type: 2,
                anim: 2,
                title: false,
                shade: shade,
                area: [i_w + '%', '100%'],
                btn: ['<i class="layui-icon">&#x1006;</i>&nbsp;关闭'],
                closeBtn: 0,
                resize: false,
                offset: 'r',
                btnAlign: 'c',
                content: url,
                yes: function(){
                    location.reload();

                }

            });
        }
        , saveOrUpdate: function (type, url, param) {
            $.ajax({
                type: type, url: url, data: param + "&_t=" + new Date().getTime()
                , success: function (data) {
                    if (data.code == "0001") {
                        layer.msg(data.message, {anim: 0, icon: 1, time: 1000});
                    } else {
                        layer.msg(data.message, {anim: 6, icon: 2, time: 3000});
                    }
                    loadData();
                }
                , error: function (data) {
                    alert(JSON.stringify(data));
                }
            });
        }, exec: function (url) {
            $.ajax({
                type: 'get', url: url, data: "_t=" + new Date().getTime()
                , success: function (data) {
                    if (data.code == "0001") {
                        layer.msg(data.message, {anim: 0, icon: 1, time: 1000});
                    } else {
                        layer.msg(data.message, {anim: 6, icon: 2, time: 3000});
                    }
                }
                , error: function (data) {
                    alert(JSON.stringify(data));
                }
            });
        }
        , del: function (url) {//信息删除
            layer.open({
                title: false, shade: shade, content: '确定删除吗？', btn: ['确定', '取消'], closeBtn: 0, btnAlign: 'c'
                , yes: function (index) {
                    $.get(url, {_t: new Date().getTime()}, function (data) {
                        if (data.code == "0001") {//成功
                            layer.msg(data.message, {anim: 0, icon: 1, time: 1000});
                        } else {//失败
                            layer.msg(data.message, {anim: 6, icon: 2, time: 3000});
                        }
                        loadData();
                        layer.close(index);
                    });
                }
            });
        }

        //异步操作
        , operator: function (url, info) {
            layer.open({
                title: false, shade: shade, content: info, btn: ['<i class="layui-icon">&#xe605;</i>&nbsp;确定', '<i class="layui-icon">&#x1006;</i>&nbsp;取消'], closeBtn: 0, btnAlign: 'c'
                , yes: function (index) {
                    $.get(url, {_t: new Date().getTime()}, function (data) {
                        if (data.code == "0001") {//成功
                            layer.msg(data.message, {anim: 0, icon: 1, time: 1000});
                        } else {//失败
                            layer.msg(data.message, {anim: 6, icon: 2, time: 3000});
                        }
                        loadData();
                        layer.close(index);
                    });
                }
            });
        }

        //内容打印
        , print: function (url, w) {
            if (w == undefined) {
                w = i_w;
            }

            layer.open({
                type: 2,
                anim: 2,
                title: false,
                shade: shade,
                area: [w + '%', '100%'],
                btn: ['<i class="layui-icon layui-icon-print"></i>&nbsp;打印', '<i class="layui-icon">&#x1006;</i>&nbsp;关闭'],
                closeBtn: 0,
                resize: false,
                offset: 'r',
                btnAlign: 'c',
                content: url,
                yes: function (index, layero) {
                    var iframeWindow = $(layero).find("iframe")[0].contentWindow;
                    iframeWindow.document.getElementById("printIframe").contentWindow.focus();    //获得焦点
                    iframeWindow.document.getElementById("printIframe").contentWindow.print();
                }
            });
        }

        , login: function () {
            layer.open({
                type: 2
                , id: 'validateIsLogin'
                , title: "用户信息已失效，请重新登录!!"
                , closeBtn: 0
                , btnAlign: 'c'
                , content: "/login/minLogin"
                , area: ['500px', '360px']
                , end: function (index, layero) {
                    //触发关闭事件后，执行此方法
                    parent.reStart();
                }
            });
        }
        , minForm: function (url, w, h) {
            if (w == undefined) {
                w = "500px";
            }
            if (h == undefined) {
                h = "360px";
            }
            layer.open({
                type: 2
                , anim: 5
                , title: false
                , closeBtn: 0
                , content: url
                , area: [w, h]
                , btn: ['确定', '关闭']
                , yes: function (index, layero) {
                    var iframeWindow = $(layero).find("iframe")[0].contentWindow;
                    var submitBtn = $(iframeWindow.document.getElementById("submitBtn"));
                    submitBtn.click();
                }
                , end: function (index, layero) {
                    //触发关闭事件后，执行此方法
                }
            });
        }
    };
    layui.link('/style/layerOpen.css');//引入css
    //输出接口
    exports('jmdf', obj);
});